package cn.edu.neosoft.sunbo509.finefood.Fragment;


import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Adapter.SearchAdapter;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.SearchBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class SearchFragment extends Fragment {
    private EditText et_search;
    private Button btn_search;
    private Service service;
    private RecyclerView rv_search;
    private RecyclerView.LayoutManager sl_layoutmanager;
    private Retrofit retrofit;
    private View view;
    private SearchAdapter adapter;
    public SearchFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_search, container, false);
        InitView();
        InitEvent();
        return view;
    }

    private void InitView() {
        et_search=(EditText) view.findViewById(R.id.et_search);
        btn_search=(Button)view.findViewById(R.id.btn_search);
        rv_search=(RecyclerView)view.findViewById(R.id.rv_search);
        sl_layoutmanager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        rv_search.setLayoutManager(sl_layoutmanager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rv_search.setHasFixedSize(true);
        adapter=new SearchAdapter(getActivity());
        rv_search.setAdapter(adapter);
    }
    private void InitEvent(){
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(et_search.getText().toString()))
                    Toast.makeText(getActivity(),"大哥，搜索内容不能为空呀",Toast.LENGTH_SHORT).show();
                else
                getDate();
            }
        });
    }
    public void getDate(){
        String name=et_search.getText().toString();
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<List<SearchBean>>call=service.searchDish(name);
        call.enqueue(new Callback<List<SearchBean>>() {
            @Override
            public void onResponse(Call<List<SearchBean>> call, Response<List<SearchBean>> response) {
                adapter.setList(response.body());
            }
            @Override
            public void onFailure(Call<List<SearchBean>> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }


}
